package com.innovate.nms.service.inspect;

import com.innovate.nms.common.Ping;
import com.innovate.nms.domain.NetNode;
import com.innovate.nms.service.channel.Channel;
import com.innovate.nms.service.command.Echo;
import com.innovate.nms.service.command.Proxy;

import lombok.Getter;

public class InspectNetTask extends InspectTask {
	
	@Getter
	private NetNode[] nodes = null;
	
	public InspectNetTask(NetNode...nodes) {
		this.nodes = nodes;
	}
	
	public InspectNetTask() {
		this.nodes = initialize();
	}
	
	protected NetNode[] initialize() {
		return null;
	} 

	@Override
	public String goal() {
		
		if(nodes == null || nodes.length == 0)
			return "没有指定网络节点";
		
		switch(nodes.length) {
		case 1:
			return String.format("检测从【本地】到【%s】的网络通信是否正常", nodes[0].getName());
		case 2:
			return String.format("检测从【%s】到【%s】的网络通信是否正常", nodes[0].getName(), nodes[1].getName());
		default:
			return "指定网络节点超过2个";
		}
	}

	@Override
	protected Result inspect() throws Exception {
		
		if(nodes == null || nodes.length == 0)
			return Result.failed;
		
		if(nodes.length == 1) {			
			return inspect(nodes[0]);
		}
		
		if(nodes.length == 2) {
			return inspect(nodes[0], nodes[1]);
		}
		
		return Result.unknown;
		
	}
	
	private Result inspect(NetNode node, NetNode node2) throws Exception {
		
		
			
		if(Channel.isChannelReady("UDP")) {
			
			if(node.isEmu()) {
			
				String from = node.getIp();
				String to = node2.getIp();
				
				try {
					Proxy c = new Proxy();
					c.setProxy(from);
					c.setTarget(to);
					c.start("UDP");
					if(c.isAttachable()) {
						return Result.succeed;
					} else {
						return Result.failed;
					}
				} catch(Exception e) {}
			
			}
			
			if(node2.isEmu()) {
				
				String from = node2.getIp();
				String to = node.getIp();
				
				try {
					Proxy c = new Proxy();
					c.setProxy(from);
					c.setTarget(to);
					c.start("UDP");
					if(c.isAttachable()) {
						return Result.succeed;
					} else {
						return Result.failed;
					}
				} catch(Exception e) {}
				
			}
			
		}
		
		if(Channel.isChannelReady("SMS")) {
			
			if(node.isEmu()) {
			
				String from = node.getSms();
				String to = node2.getIp();
				
				try {
					Proxy c = new Proxy();
					c.setProxy(from);
					c.setTarget(to);
					c.start("SMS");
					if(c.isAttachable()) {
						return Result.succeed;
					} else {
						return Result.failed;
					}
				} catch(Exception e) {}
			
			}
			
			if(node2.isEmu()) {
				
				String from = node2.getSms();
				String to = node.getIp();
				
				try {
					Proxy c = new Proxy();
					c.setProxy(from);
					c.setTarget(to);
					c.start("SMS");
					if(c.isAttachable()) {
						return Result.succeed;
					} else {
						return Result.failed;
					}
				} catch(Exception e) {}
				
			}
			
		}
		
		
		return Result.unknown;
		
	}
	
	private Result inspect(NetNode node) throws Exception {
		
		if(node.isEmu()) {
			
			if(Channel.isChannelReady("UDP")) {
				
				try {
					Echo echo = new Echo();
					echo.setTarget(node.getIp());
					echo.start("UDP");
					return Result.succeed;
				} catch(Exception e) {
					return Result.failed;
				}
				
			}
			
			if(Channel.isChannelReady("SMS")) {
				
				try {
					Echo echo = new Echo();
					echo.setTarget(node.getSms());
					echo.start("SMS");
					return Result.succeed;
				} catch(Exception e) {
					return Result.failed;
				}
				
			}
			
			
			
		} else {
			
			boolean r = Ping.ping(node.getIp(), 3000);
			if(r) {
				return Result.succeed;
			} else {
				return Result.failed;
			}
			
		}		
		
		
		return Result.unknown;

		
	}



}
